﻿using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using WPFExcelReport.Helpers;
using INTEX.DataBase.Tools;
using System.IO;
namespace WPFExcelReport
{
    public partial class ITXEXRPT
    {
        private void YGRABFB(DataSet db)
        {

            try
            {
                string value = "";
                DataRow row = null;
                int x = 1, y = 1;
                x = 3; y = 5;
                string str1 = "", str2 = "", str3 = "";
                DataRow dwOrder = db.Tables["ABPOS"].Rows[0];
                string HMKRS = dwOrder["ABPAUFKRS"].ToString().Trim();
                string HMLFD = dwOrder["ABPAUFLFD"].ToString().Trim();
                string HMPOS = dwOrder["ABPAUFPOS"].ToString().Trim();
                //string APKSPEZIF = GetGB2312Value(dwOrder["APKSPEZIF"].ToString().Trim());
                double ABPSTKLG = Convert.ToDouble(dwOrder["ABPSTKLG"].ToString()) / 10000000;
                double ABPSOLLMENGE7 = Convert.ToDouble(dwOrder["ABPSOLLMENGE7"].ToString()) / 100;
                double ABPSOLLMENGE5 = Convert.ToDouble(dwOrder["ABPSOLLMENGE5"].ToString()) / 100;
                if (ABPSTKLG == 0)
                    ABPSTKLG = 1;
                if (ABPSOLLMENGE7 == 0)
                    ABPSOLLMENGE7 = ABPSOLLMENGE5;
                double TarGetRatio = Math.Round(ABPSOLLMENGE5 * ABPSTKLG / ABPSOLLMENGE7, 2);
                if (TarGetRatio == 0)
                    TarGetRatio = 1;
                value = HMKRS + "-" + Helper.GetSubRollNum(HMLFD) + "-" + Helper.GetSubRollNum(HMPOS);
                exAgent.SetCellValue(3, 5, value);
                string 生产单条码 = "*" + HMKRS + HMLFD + HMPOS + "01*";
                exAgent.SetCellValue(2, 22, 生产单条码);
                exAgent.SetCellValue(1, 2, "复板单(" + value + ")");
                x = 3; y = 30;
                Helper.GetDate(ref date, ref year, ref month, ref day);
                Helper.GetSplitDate(ref str1, ref str2, ref str3, date);
                exAgent.SetCellValue(2, 37, str3);
                int positions = 0;
                if (!db.Tables.Contains("ABAUFM"))
                {
                    return;
                }
                string ABPART, ABPVAR, ABPFARBE;

                ABPART = dwOrder["ABPART"].ToString();
                ABPVAR = dwOrder["ABPVAR"].ToString();
                ABPFARBE = dwOrder["ABPFARBE"].ToString();
                exAgent.SetCellValue(4, 14, ABPFARBE.Trim());  
                string MU = "";
                string TY = ""; 
                int idx = 0; 
                exAgent.SetCellValue(5, 5, TarGetRatio);
              

                DataRow dwData = INTEXDB.ICC_QUERYTB.NewRow();
                dwData["ABAMANDANT"] = app.Mandant;
                dwData["ABAAUFKRS"] = HMKRS;
                dwData["ABAAUFLFD"] = HMLFD;
                dwData["ABAAUFPOS"] = HMPOS;
                DataSet SResult = INTEXDBC.QueryABAUFM(dwData, 11);
                DataTable TBMU = SResult.Tables[0].Copy();
                var query = from DataRow dwRow in TBMU.AsEnumerable()
                            where dwRow["ABAVKAUFTYP"].ToString() == "K"
                            select dwRow;
                idx = 0; 


                double kg = 0;
                x = 0; 
                int nRow = 2;
                int xStart = 36;
                idx = 0;
                DataRow dwGAAUF = null;
                Dictionary<int, string> EachBatch = new Dictionary<int, string>();

                int count = db.Tables["GAAUF"].Rows.Count;
                var qGA = from DataRow ga in db.Tables["GAAUF"].AsEnumerable() 
                          select ga;
                foreach (DataRow ga in qGA)
                {
                    dwGAAUF = ga;
                    string GSGARNBEZ =GetGB2312Value(dwGAAUF["GSGARNBEZ"].ToString());
                    MU = dwGAAUF["GAAUFMSL"].ToString();
                    double GABASMG2 = Convert.ToDouble(dwGAAUF["GABASMG2"].ToString()) / 1000; 
                    exAgent.SetCellValue(xStart + idx * nRow, 17, MU);
                    exAgent.SetCellValue(xStart + idx * nRow, 19, ABPART.Trim());// + ABPVAR.Trim());
                     exAgent.SetCellValue(xStart + idx * nRow + 1, 19, GetGB2312Value(ABPFARBE.Trim()));
                    if (MU != "特殊原料")
                    {
                        exAgent.SetCellValue(xStart + idx * nRow, 24, GetGB2312Value(dwGAAUF["GABEM"].ToString().Trim()));
                    }
                    else
                    {
                        exAgent.SetCellValue(xStart + idx * nRow, 24, GetGB2312Value(dwGAAUF["GABEM"].ToString().Trim()) + "/" + TY);
                    }

                    exAgent.SetCellValue(xStart + idx * nRow, 38, kg);
                    double GASP1 = Convert.ToDouble(dwGAAUF["GASP1"].ToString()) / 1000;
                    if (GASP1 == 0)
                    {
                        exAgent.SetCellValue(xStart + idx * nRow, 33, Math.Round(GABASMG2 / ABPSTKLG, 2) + "%");
                    }
                    else
                    {
                        exAgent.SetCellValue(xStart + idx * nRow, 33, Math.Round(GASP1, 2) + "%");
                    }

                    #region=====Alloction=======
                    Dictionary<string, double> ListZu = new Dictionary<string, double>();

                    var qzu = from FP in db.Tables["ZUTEI"].AsEnumerable()
                              where FP["ZUPOS"].ToString() == dwGAAUF["GAPOS"].ToString()
                              select FP;

                    double ZUEINTKG = 0;
                    double QTYZU = 0;
                    foreach (DataRow rw in qzu)
                    {
                        TY = GetGB2312Value(rw["ZUSPPART"].ToString().Trim());
                        string sppartie = rw["ZUSPPART"].ToString().Trim();
                         exAgent.SetCellValue(xStart + idx * nRow, 19, GSGARNBEZ);// + ABPVAR.Trim());
                        string LPBEZ = GetGB2312Value(rw["ZULB"].ToString().Trim());
                        ZUEINTKG = Convert.ToDouble(rw["ZUEINTKG"].ToString()) / 1000;
                        if (ListZu.ContainsKey(LPBEZ))
                        {
                            QTYZU = ListZu[LPBEZ];
                            QTYZU += ZUEINTKG;
                        }
                        else
                        {
                            QTYZU = ZUEINTKG;
                            ListZu.Add(LPBEZ, QTYZU);
                        }
                    }

                    string allocation = "";
                    int stkcount = ListZu.Count;
                    foreach (KeyValuePair<string, double> kp in ListZu)
                    {
                        if (stkcount > 1)
                        {
                            allocation += kp.Key + "(" + kp.Value.ToString() + ")";
                        }
                        else
                        {
                            allocation += kp.Key;
                        }
                    }

                    exAgent.SetCellValue(xStart + idx * nRow, 27, allocation);
                    #endregion
                    idx++;
                    if (idx < count)
                    {
                        exAgent.RangeCopy(xStart, xStart + 1, xStart + idx * nRow, xStart + idx * nRow + 1);
                    }
                } 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void BackUpFB(DataSet db)
        {
            try
            {
                if (!System.IO.Directory.Exists("T:"))
                {
                    return;
                }
                if (!DestFile.Contains("复板单"))
                    return;
                string tempfile = DestFile.Replace("C:\\IntexReport\\复板单", "T:\\YGRABFB");
                if (File.Exists(tempfile))
                {
                    throw new Exception(tempfile + "已经存在,禁止重复打印");
                }
                File.Copy(DestFile, tempfile);
                string content = DataToXml.CDataToXml(db);
                DataRow dwLog = VLOG.TBMain.NewRow();
                VLOG.SetUpNewRow(ref dwLog);
                dwLog["ICCTABLENAME"] = "YGRABFB";
                dwLog["CONTENTS"] = content.Substring(0, 3500);
                VLOG.InsertICC_LOG(dwLog);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}